// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import config from "../config/config";
import func from "../config/func";
import AdManage from "../manage/AdManage";
import AudioManage from "../manage/AudioManage";

const { ccclass, property } = cc._decorator;

@ccclass
export default class TouchByMistake extends cc.Component {

    @property(cc.Node)
    node_charge: cc.Node = null;
    @property(cc.Node)
    node_speed: cc.Node = null;
    @property(cc.Node)
    node_reward: cc.Node = null;
    @property(cc.Node)
    node_tip: cc.Node = null;

    n_createBanner: boolean = false;
    n_touchBtn: boolean = true;
    n_cook1: cc.Animation = null;
    n_cook2: cc.Animation = null;

    n_touch: cc.Node = null;
    n_chargeTotal: cc.Node = null;
    n_chargeFinish: cc.Sprite = null;
    n_chargeNum: cc.Label = null;

    n_rewardBox: cc.Node = null;
    n_rewardFinish: cc.Sprite = null;
    n_quan: cc.Node = null;
    n_rewardText: cc.Label = null;
    n_boxAni: boolean = false;
    n_creatAppBox: boolean = false;

    onLoad() {
        this.n_cook1 = this.node_charge.getChildByName("cook1").children[0].getComponent(cc.Animation);
        this.n_cook1.on(cc.Animation.EventType.FINISHED, () => {
            this.n_cook1.node.active = false;
        }, this);
        this.n_cook2 = this.node_charge.getChildByName("cook2").children[0].getComponent(cc.Animation);
        this.n_cook2.on(cc.Animation.EventType.FINISHED, () => {
            this.n_cook2.node.active = false;
        }, this);
        this.n_chargeTotal = this.node_charge.getChildByName("total");
        this.n_chargeFinish = this.n_chargeTotal.children[0].getComponent(cc.Sprite);
        this.n_touch = this.node_charge.getChildByName("btn1");
        this.n_chargeNum = this.node_charge.getChildByName("num").getComponent(cc.Label);

        this.n_rewardBox = this.node_reward.getChildByName("box");
        this.n_rewardBox.on(cc.Node.EventType.TOUCH_END, this.touchBox, this);
        this.n_rewardFinish = this.node_reward.getChildByName("total").children[0].getComponent(cc.Sprite);
        this.n_quan = this.node_tip.getChildByName("quan");
        this.n_rewardText = this.node_tip.getChildByName("text").getComponent(cc.Label);
        this.node_tip.on(cc.Node.EventType.TOUCH_END, () => {
            this.n_quan.stopAllActions();
            this.close();
        }, this)
    }
    init(type: number) {
        config.openPopup = false;
        this.node_tip.active = false;
        this.n_touchBtn = true;
        if (type == 1) {
            func.destroyBanner();
            config.qq_stopTime = true;
            this.node_charge.active = true;
            this.node_speed.active = false;
            this.node_reward.active = false;
            // if (AdManage.instance.n_blockAd) {
            //     AdManage.instance.n_blockAd.destroy();
            // }
            this.n_chargeFinish.fillRange = 0;
            this.n_createBanner = false;
            this.n_touch.y = -cc.winSize.height / 2 + this.n_touch.height / 2;
        } else if (type == 2) {
            this.node_charge.active = false;
            this.node_speed.active = true;
            this.node_reward.active = false;
        } else {
            this.node_charge.active = false;
            this.node_speed.active = false;
            this.node_reward.active = true;
            this.n_rewardFinish.fillRange = 0;
            this.n_creatAppBox = false;
        }
    }
    /**充电 */
    touchCharge() {
        if (!this.n_touchBtn) return;
        AudioManage.audioManage.playEffectMusic(config.soundPath.click);
        if (!this.n_cook1.node.active) {
            this.n_cook1.node.active = true;
            this.n_cook1.play();
        }
        if (!this.n_cook2.node.active) {
            this.n_cook2.node.active = true;
            this.n_cook2.play();
        }
        this.n_chargeFinish.fillRange += 0.2;
        if (this.n_chargeFinish.fillRange >= 1) {
            this.n_touchBtn = false;
            this.close();
        } else if (this.n_chargeFinish.fillRange >= 0.4 && !this.n_createBanner) {
            this.n_createBanner = true;
            func.creatrBanner();
            this.schedule(() => {
                func.destroyBanner();
                setTimeout(() => {
                    func.creatrBanner();
                }, 500);
            }, 10)
            setTimeout(() => {
                this.n_touch.y += this.n_touch.height;
            }, 1000);
        }
    }
    /**点击盒子 */
    touchBox() {
        AudioManage.audioManage.playEffectMusic(config.soundPath.click);
        if (!this.n_boxAni) {
            this.n_boxAni = true;
            cc.tween(this.n_rewardBox)
                .by(0.1, { angle: 10 })
                .by(0.1, { angle: -20 })
                .by(0.1, { angle: 10 })
                .call(() => {
                    this.n_boxAni = false;
                })
                .start()
        }
        this.n_rewardFinish.fillRange += 0.1;
        if (this.n_rewardFinish.fillRange >= 0.4 && !this.n_creatAppBox) {
            this.n_creatAppBox = true;
            AdManage.instance.initQQcreateAppBox();
        } else if (this.n_rewardFinish.fillRange >= 1) {
            this.node_reward.active = false;
            this.getReward();
        }
    }
    /**试用 */
    touchOnTrial() {
        AudioManage.audioManage.playEffectMusic(config.soundPath.click);
        AdManage.instance.initRewardedVideoAd(config.qq_videoAd, (res) => {
            if (res == 1) {
                config.addSpeed = 2;
                this.node.destroy();
            }
        })
    }
    /**不试用 */
    noOnTrial() {
        AudioManage.audioManage.playEffectMusic(config.soundPath.click);
        if (config.qq_loadCfg) {
            if (config.qq_loadCfg.mistouchOn == 0 || (config.qq_loadCfg.mistouchOn == 1 && config.qq_onTrial)) {
                config.addSpeed = 1;
                this.close();
            } else {
                config.qq_onTrial = true;
                AdManage.instance.initRewardedVideoAd(config.qq_videoAd, (res) => {
                    if (res == 1) {
                        config.addSpeed = 2;
                    }else if(res == 2){
                        config.addSpeed = 1;
                    }
                    this.close();
                })
            }
        } else {
            config.addSpeed = 1;
            this.close();
        }
    }
    /**获取奖励 */
    getReward() {
        this.node_tip.active = true;
        cc.tween(this.n_quan)
            .repeatForever(
                cc.tween()
                    .by(3, { angle: 360 })
            )
            .start()
        let money = (config.checkPoint - 1) * 20;
        this.n_rewardText.string = `获得 x ${money}`;
        config.money += money;
    }

    close() {
        if (this.node_charge.active) {
            this.init(2);
        } else if (this.node_speed.active) {
            config.qq_stopTime = false;
            this.node.destroy();
        } else {
            this.node.destroy();
        }
    }
    lateUpdate(dt: number) {
        this.n_chargeNum.string = `当前电量:${Math.floor(this.n_chargeFinish.fillRange * 100)}%`;
        if (this.n_touchBtn) {
            if (this.node_charge.active) {
                if (this.n_chargeFinish.fillRange > 0) {
                    this.n_chargeFinish.fillRange -= dt / 3;
                    this.n_chargeFinish.fillRange = this.n_chargeFinish.fillRange < 0 ? 0 : this.n_chargeFinish.fillRange;
                }
            } else if (this.node_reward.active) {
                if (this.n_rewardFinish.fillRange > 0) {
                    this.n_rewardFinish.fillRange -= dt / 3;
                    this.n_rewardFinish.fillRange = this.n_rewardFinish.fillRange < 0 ? 0 : this.n_rewardFinish.fillRange;
                }
            }
        }

    }
}
